package com.library.framework.manager;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: BizCodeUtils
 * @Description: 业务Code生成
 * @Author: liyanyong
 * @Date: 2021年09月24日 11:11
 */
@Configuration
public class BizCodeManager {
    /**
     * 位数
     */
    private final static int BIT_NUM_WL = 4;

    private final static String XQ = "SP";
    private final static String AT = "AT";
    /**
     * 活动编号
     */
    private final static String HD = "HD";
    /**
     * Banner
     */
    private final static String BANNER = "BR";
    /**
     * 绘本
     */
    private final static String HB = "B";

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 生产商品编码
     * SP-20220222-0001
     *
     * @return SP-20220222-0001
     */
    public String createSp() {
        String date = this.getDate();
        String redisKey = String.format("%s-%s", XQ, date);
        String after = this.getAddZero(this.incrBy(redisKey, 1L));
        return String.format("%s%s", redisKey, after);
    }

    /**
     * 活动分类编号
     *
     * @return
     */
    public String createAt() {
        String date = this.getDate();
        String redisKey = String.format("%s-%s", AT, date);
        String after = this.getAddZero(this.incrBy(redisKey, 1L));
        return String.format("%s%s", redisKey, after);
    }

    /**
     * banner管理
     *
     * @return
     */
    public String createBannerNo() {
        String date = this.getDate();
        String redisKey = String.format("%s-%s", BANNER, date);
        String after = this.getAddZero(this.incrBy(redisKey, 1L));
        return String.format("%s%s", redisKey, after);
    }

    /**
     * 活动编号
     *
     * @return
     */
    public String createHd() {
        String date = this.getDate();
        String redisKey = String.format("%s-%s", HD, date);
        String after = this.getAddZero(this.incrBy(redisKey, 1L));
        return String.format("%s%s", redisKey, after);
    }

    public String createHb() {
        String date = this.getDate();
        String redisKey = String.format("%s-%s", HB, date);
        String after = this.getAddZero(this.incrBy(redisKey, 1L));
        return String.format("%s%s", redisKey, after);
    }

    /**
     * @Title: incrBy
     * @Description: 增加(自增长), 负数则为自减
     * @Author: liyanyong
     * @Date: 2021年09月24日 16:43:19
     * @Param: key
     * @Param: increment
     * @Return: java.lang.Long
     */
    public Long incrBy(String key, long increment) {
        Long value = redisTemplate.opsForValue().increment(key, increment);
        if (value <= 1) {
            this.expire(key, 1L, TimeUnit.DAYS);
        }
        return value;
    }

    /**
     * @Title: expire
     * @Description: 设置过期时间
     * @Author: liyanyong
     * @Date: 2021年09月24日 16:44:10
     * @Param: key
     * @Param: timeout
     * @Param: unit
     * @Return: java.lang.Boolean
     */
    public Boolean expire(String key, long timeout, TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }

    /**
     * @Title: getDate
     * @Description: 获取年月日时间
     * @Author: liyanyong
     * @Date: 2021年09月24日 16:43:55
     * @Return: java.lang.String
     */
    private String getDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        return simpleDateFormat.format(new Date());
    }

    /**
     * @Title: getAddZero
     * @Description: 补充零
     * @Author: liyanyong
     * @Date: 2021年09月24日 15:39:51
     * @Param: value      数值
     * @Return: java.lang.String
     */
    private String getAddZero(long value) {
        return this.getAddZero(BIT_NUM_WL, value);
    }

    /**
     * @Title: getAddZero
     * @Description: 补充零
     * @Author: liyanyong
     * @Date: 2021年09月24日 15:39:51
     * @Param: zeroNum    补充几个0，默认 BIT_NUM
     * @Param: value      数值
     * @Return: java.lang.String
     */
    private String getAddZero(Integer zeroNum, long value) {
        zeroNum = (null == zeroNum) ? BIT_NUM_WL : zeroNum;
        return String.format("%0" + zeroNum + "d", value);
    }


}
